package org.spiderflow;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.spiderflow.core.executor.function.StringFunctionExecutor;
import org.spiderflow.core.model.SplitText;
import org.spiderflow.core.utils.StringUtils;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TestSelenium {
    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver","E:/driver/chromedriver.exe");

        //ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File("E:/driver/chromedriver.exe")).usingAnyFreePort().build();
        //service.start();

        String url="https://translate.google.cn/?sl=en&tl=zh-CN&text=test&op=translate";

        //打开一个浏览器
        //chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"
        cmd("chrome.exe --remote-debugging-port=9222 --user-data-dir=\"C:\\selenum\\AutomationProfile\" --new-window \"https://translate.google.cn/?sl=en&tl=zh-CN&text=test&op=translate\" ");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--no-sandbox");
        //options.addArguments("--headless");
        options.addArguments("--disable-infobars");
        options.addArguments("--blink-settings=imagesEnabled=false");
        //options.addArguments("--proxy-server=http://127.0.0.1:8080");
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
        ChromeDriver driver;
        driver = new ChromeDriver(options);
        //driver.get(url);
        //Thread.sleep(1000L);
        clearOldText(driver);
        transText(driver,"\"To promote growth of the sector means a lot to boosting consumption, improving livelihoods and creating more jobs,\" he said.");
        clearOldText(driver);
        transText(driver,"The conference was held to explain a guideline released last month by the central government to boost development of the sector and improve service quality");
        clearOldText(driver);
        transText(driver,"Twelve boys and their football coach trapped in a Thai cave have been found alive but will need learn to dive or wait months for flooding to recede before they can get out, the army says.");
        //driver.manage().deleteAllCookies();
        //WebElement textArea = driver.findElementByCssSelector("textarea.er8xn");

        //textArea.sendKeys(Keys.chord(Keys.CONTROL, "v"));
//        try {
//            Thread.sleep(3000000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        driver.quit();

          String sentence = "The common varieties sold for an average of about 1,300 yuan ($200) a metric ton this year, but the new variety can fetch an extra 100 to 200 yuan, Zhang said. \"The new variety has gained popularity among our clients and we have received orders till the end of next year.\"";
//        SplitText.TextType result =  StringUtils.checkSpecialSplitChar(sentence);
//        System.out.println(result.toString());
//        System.out.println("******************************************");
//        List<String> resultList = StringFunctionExecutor.splitSpecialSentence(sentence,"\\!|\\.|\\?|。|？|！");
//        resultList.forEach(System.out::println);
//        System.out.println("******************************************");
//        List<String> resultList2 = StringFunctionExecutor.splitSentence("I have. a dream? yes!","\\!|\\.|\\?|。|？|！");
//        resultList2.forEach(System.out::println);

    }

    private static void clearOldText(ChromeDriver driver){
        WebElement clearButton = driver.findElementByCssSelector("[jsname='X5DuWc']");
        clearButton.click();
    }

    private static void transText(ChromeDriver driver,String text){
        try {
            WebElement textArea = driver.findElementByCssSelector("textarea.er8xn");
            String js = "document.querySelector('textarea.er8xn').value='"+text+"';";
            //String js="alert(123)";
            JavascriptExecutor j = (JavascriptExecutor) driver;
            j.executeScript(js,textArea);

            Actions actions = new Actions(driver);
            actions.moveToElement(textArea).sendKeys(" ").sendKeys(Keys.ENTER).perform();
            Thread.sleep(2000);
            List<WebElement> resultSpans = driver.findElementsByCssSelector("[jsname='W297wb']");
            resultSpans.forEach((e) -> {
                System.out.println(e.getText());
            });
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    public static boolean cmd(String command){
        boolean flag = false;
        try{
            Runtime.getRuntime().exec("cmd.exe /C start "+command);
            flag = true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

}
